package cn.wawi.controller.sys;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import cn.wawi.dao.sys.ResourcesDao;
import cn.wawi.entity.sys.Resources;
import cn.wawi.entity.sys.User;

@Controller
public class IndexController{
	@Resource
	protected ResourcesDao<Resources> resourcesDao;
	@Resource
	private Producer captchaProducer;
	
	/**
	 *登录页面
	 */
	@RequestMapping(value="/a",method={RequestMethod.GET,RequestMethod.POST})
	public String login() {
		return "core/login";
	}
	/**
	 *首页面
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	@SuppressWarnings("all")
	public String index(HttpServletRequest request) {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		//List<Resources> list= (List<Resources>) SecurityUtils.getSubject().getSession().getAttribute("menuList");
		//if(list==null){
		List<Resources> list=resourcesDao.getUserPermission(user.getId(),1);
			//SecurityUtils.getSubject().getSession().setAttribute("menuList", list);
		//}
		request.setAttribute("menuList", list);
		return "core/index";
	}
	@RequestMapping(value="/public/images/kaptcha")
	public ModelAndView initKaptcha(HttpServletRequest request,HttpServletResponse response){
		response.setDateHeader("Expires",0);
		response.setHeader("Cache-Control","no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control","post-check=0, pre-check=0");
		response.setHeader("Pragma","no-cache");
		response.setContentType("image/jpeg");
		String capText=captchaProducer.createText();
		request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);
		BufferedImage bi =captchaProducer.createImage(capText);
		ServletOutputStream out=null;
        try {
        	out=response.getOutputStream();
            ImageIO.write(bi,"jpg",out);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
}
